<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader />
        <MyList />
        <MyFooter />
      </div>
    </div>
  </div>
</template>

<script>
import MyHeader from "../components/todolist/MyHeader.vue";
import MyList from "../components/todolist/MyList.vue";
import MyFooter from "../components/todolist/MyFooter.vue";
import starDemo from "../components/todolist/star-demo.vue";
export default {
  name: "List",
  components: { MyHeader, MyList, MyFooter, starDemo },
  data() {
    return {};
  },
  methods: {
    //     思路：
    // 1、从数组arr中取出 p_id = 0 的数据 arr_pid0 存到数组中
    // 2、然后找他的children，怎么找children呢？
    // 3、假设arr_pid0 的 id 为 1，那么我要从arr中取出p_id = 1的数据赋值给children就可以了
    // 4、然而咱们的函数就是从arr中取出 p_id === rootValue 的值
    // 5、所以const children = newArrFn(arr, cur.id)
    // 6、cur.children = children
    newArrFn(arr, rootValue = 0) {
      return arr.reduce((acc, cur) => {
        if (cur.p_id === rootValue) {
          const children = newArrFn(arr, cur.id);
          if (children.length) {
            cur.children = children;
          }
          acc.push(cur); // 返回值 新数组的长度
        }
        return acc;
      }, []);
    },
  },
  computed: {
    todosLists() {
      return this.$store.state.listAbout.todosLists;
    },
  },
  watch: {
    todosLists: {
      deep: true,
      handler(value) {
        localStorage.setItem("todosLists", JSON.stringify(value));
      },
    },
  },
  mounted() {
    this.$store.commit("listAbout/SetUserId", this.$route.query.userId);
    this.$store.commit("listAbout/setListId", this.$route.query.userId);
    let arr = [
      { id: 1, p_id: 0, name: "首页" },
      { id: 2, p_id: 0, name: "菜单管理" },
      { id: 3, p_id: 0, name: "菜单列表" },
      { id: 4, p_id: 1, name: "权限管理" },
      { id: 5, p_id: 6, name: "管理员列表" },
      { id: 6, p_id: 4, name: "角色列表" },
    ];
    this.newArrFn(arr, 0);
  },
};
</script>

<style>
#root {
  align-items: center;
  /* 让页面始终占浏览器可视区域总高度 */
  height: 100vh;
  /* 背景渐变色 */
  background: linear-gradient(#141e30, #243b55);
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 800px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  position: absolute;
  background-color: #ffdcc2;
  margin-top: 20px;
  opacity: 0.9;
  box-shadow: 0px 3px 20px 3px #ffdcc2;
}
</style>